﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _0512301_0512430.Main
{
    public partial class frmMain : Form
    {
        public static Boolean btnTopSearchClicked = false;
        public static Boolean btnTopPlacesClicked = false;
        public static Boolean btnTopLayersClicked = false;

        private int heightMenu = 30;
        private int spaceBetweenTwoButton = 10;

        public static string strFind = "ho+chi+minh";

        public frmMain()
        {
            InitializeComponent();
            strFind = "ha+noi";
        }

        private String[] findPlaces(String keyFound)
        {
            String[] strResult = new String[1000];
            return strResult;
        }
        // Thêm danh sách địa điểm được tìm thấy vào cây.
        private void addNodesFound(String[] strFound)
        {
            TreeNode[] arrTree = new TreeNode[strFound.Length];

            // Thêm tất cả kết quả tìm kiếm được vào cây
            for (int i = 0; i < strFound.Length; i++)
            {
                arrTree[i] = new TreeNode(strFound[i]);

                arrTree[i].Name = strFound[i];
                arrTree[i].Text = strFound[i];
                if(this.treeViewPlaces.Nodes.Find(arrTree[i].ToString(), true) != null)
                {
                    this.treeViewPlaces.Nodes.AddRange(new TreeNode[] {
            arrTree[i]});
                }
            }

        }
        
        // Tìm địa điểm con trong 1 Node cha.
        private void addChildNodesFound(String[] strFound, TreeNode parentNode)
        {
            for (int i = 0; i < strFound.Length; i++)
            {
                parentNode.Nodes.Add(strFound[i]);
            }
        }
        private void btnTopPlace_Click(object sender, EventArgs e)
        {
            
            btnTopPlacesClicked = btnTopPlacesClicked ? false : true ;

            if (btnTopPlacesClicked)
            {
                this.pnlPlaces.Height = 34;
                
                this.btnTopPlace.Image = global::_0512301_0512430.Properties.Resources.Forward;
            }
            else
            {
                this.pnlPlaces.Height = 303;
                if (this.pnlLayers.Height == 34)
                {
                    this.treeViewPlaces.Height = this.Height - this.pnlSearch.Height - this.pnlLayers.Height - heightMenu - 100;
                    this.pnlPlaces.Height = this.treeViewPlaces.Height + this.btnTopPlace.Height + 15;
                    this.pnlLayers.Location = new System.Drawing.Point(5, (this.pnlPlaces.Height + this.pnlSearch.Height + spaceBetweenTwoButton + heightMenu));
                }

                this.btnTopPlace.Image = global::_0512301_0512430.Properties.Resources.Down;
            }
            this.pnlLayers.Location = new System.Drawing.Point(5, (this.pnlPlaces.Height + this.pnlSearch.Height + spaceBetweenTwoButton + heightMenu));

        }

        private void btnTopSearch_Click(object sender, EventArgs e)
        {
            btnTopSearchClicked = btnTopSearchClicked ? false : true;

            if (btnTopSearchClicked)
            {
                this.pnlLayers.Height += this.pnlSearch.Height - 30;
                this.pnlSearch.Height = 30;

                this.btnTopSearch.Image = global::_0512301_0512430.Properties.Resources.Forward;
            }
            else
            {
                this.pnlSearch.Height = 146;
                this.pnlLayers.Height -= this.pnlSearch.Height - 30;

                this.btnTopSearch.Image = global::_0512301_0512430.Properties.Resources.Down;
            }
            this.pnlLayers.Location = new System.Drawing.Point(5, (this.pnlPlaces.Height + this.pnlSearch.Height + spaceBetweenTwoButton + heightMenu));
            this.pnlPlaces.Location = new System.Drawing.Point(5, (this.pnlSearch.Height + spaceBetweenTwoButton + heightMenu));
        }

        private void btnLabelLayers_Click(object sender, EventArgs e)
        {
            btnTopLayersClicked = btnTopLayersClicked ? false : true;

            if (btnTopLayersClicked)
            {
                this.pnlLayers.Height = 34;
                this.pnlLayers.Location = new System.Drawing.Point(5, (this.Height - 70));
                this.pnlPlaces.Height += this.pnlLayers.Height - 34;
                this.treeViewPlaces.Height = this.Height - this.pnlSearch.Height - this.pnlLayers.Height - heightMenu - 100;
                this.pnlPlaces.Height = this.treeViewPlaces.Height + this.btnTopPlace.Height + 15;

                this.btnTopLayers.Image = global::_0512301_0512430.Properties.Resources.Forward;
            }
            else
            {
                this.pnlLayers.Height = 176;
                this.pnlPlaces.Height = 303;
                this.treeViewPlaces.Height = this.pnlPlaces.Height - 42;
                this.pnlLayers.Location = new System.Drawing.Point(5, (this.pnlPlaces.Height + this.pnlSearch.Height + spaceBetweenTwoButton + heightMenu));

                this.btnTopLayers.Image = global::_0512301_0512430.Properties.Resources.Down;
            }
        }

        private void btnZoomIn_Click(object sender, EventArgs e)
        {
            ucMaps1.ZoomIn();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form dlgExit = new dlgBox.dlgExit();
            dlgExit.ShowDialog();

            dlgBox.dlgExit.isOkClicked = dlgBox.dlgExit.isOkClicked ? true : false;
            if (dlgBox.dlgExit.isOkClicked)
            {
                ucMaps1.threadStopLoadImage();
                this.Close();
            }
        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form AboutUs = new dlgBox.AboutUs();
            AboutUs.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.AppStarting;

            if (!Internet.Internet.IsConnectedToInternet())
            {
                MessageBox.Show(this, "Can not connect to Internet!", "Google Map API", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            strFind = this.cbxSearch.Text;

            ucMaps1.findMaps();

            addNodesFound(new string[] {strFind});
            this.Cursor = Cursors.Arrow;
        }

        private void cbxSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnSearch_Click(sender, e);

                this.cbxSearch.Items.AddRange(new object[] {
            this.cbxSearch.Text});
            }
        }

        private void clearPlacesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.treeViewPlaces.Nodes.Clear();
        }

        private void treeViewPlaces_MouseHover(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void treeViewPlaces_MouseHover_1(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void ucMaps1_MouseHover(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void ucMaps1_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

        private void treeViewPlaces_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }

        private void btnZoomOut_Click(object sender, EventArgs e)
        {
            ucMaps1.ZoomOut();
        }

        private void ucMaps1_MouseDown(object sender, MouseEventArgs e)
        {
            
        }
        private void ucMaps1_MouseUp(object sender, MouseEventArgs e)
        {
            
        }

        private void ucMaps1_MouseClick(object sender, MouseEventArgs e)
        {
            //this.Cursor = new Cursor(Cursor.Current.Handle);
            //Cursor.Position.
           
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            ucMaps1.MoveLeft();
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            ucMaps1.MoveRight();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            ucMaps1.MoveUp();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            ucMaps1.MoveDown();
        }
    }
}
